package com.zyd.service.role.impl;

import com.github.pagehelper.PageHelper;
import com.zyd.constants.ErrorDef;
import com.zyd.exception.BusinessException;
import com.zyd.io.model.RoleEntity;
import com.zyd.io.model.RoleEntityExample;
import com.zyd.io.model.RoleEntityKey;
import com.zyd.io.page.PageBean;
import com.zyd.mappers.RoleEntityMapper;
import com.zyd.service.role.RoleService;
import com.zyd.util.SqlUtils;
import com.zyd.util.StringUtils;
import com.zyd.util.ToolUtils;
import com.zyd.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.zyd.context.HeaderContext;

import java.util.List;

@Component
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleEntityMapper mapper;

    @Override
    public RoleEntity findRole(String id) {
        RoleEntityKey key = new RoleEntityKey();
        key.setId(id);
        return mapper.selectByPrimaryKey(key);
    }

    @Override
    public PageBean<RoleEntity> findRoleList(String name, int currentPage, int pageSize) {
        String uid=(String) HeaderContext.get().get("userid");

        RoleEntityExample example = new RoleEntityExample();
        if (StringUtils.isNotBlank(name)) {
            example.or().andCodeLike(SqlUtils.andLike(name)).andDrEqualTo(0);

            example.or().andNameLike(SqlUtils.andLike(name)).andDrEqualTo(0);
        } else {
            example.or().andRpowerLike(SqlUtils.andLike(uid)).andDrEqualTo(0);
        }
        int count = (int) mapper.countByExample(example);

        PageBean<RoleEntity> pageData = new PageBean<RoleEntity>(currentPage, pageSize, count);
        PageHelper.startPage(currentPage, pageSize);
        List<RoleEntity> ret = mapper.selectByExample(example);
        pageData.setItems(ret);

        return pageData;
    }

    @Override
    public void updateRole(RoleEntity role) {
        checkRoleNameExist(role);
        role.setTs(ToolUtils.getNowTime());

        mapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public void deleteRole(String id) {
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setId(id);
        roleEntity.setDr(1);
        roleEntity.setTs(ToolUtils.getNowTime());

        mapper.updateByPrimaryKeySelective(roleEntity);
    }

    @Override
    public void addRole(RoleEntity role) {
        String uid=(String) HeaderContext.get().get("userid");
        checkRoleNameExist(role);
        role.setId(UUIDUtil.getUUid());
        role.setDr(0);
        role.setTs(ToolUtils.getNowTime());
        role.setRpower(uid);
        mapper.insertSelective(role);
    }

    private void checkRoleNameExist(RoleEntity role) {
        RoleEntityExample example = new RoleEntityExample();
        example.or().andNameEqualTo(role.getName()).andDrEqualTo(0);
        long count = mapper.countByExample(example);
        if (count > 0) {
            throw new BusinessException(ErrorDef.ROLE_NAME_EXIST_ERROR, ErrorDef.ROLE_NAME_EXIST_ERROR_MSG);
        }
    }
}
